 
import { Math2D } from "algorithm/Math2d";
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
 
import { MainStrategy } from "GameLoop/WorldState/EntityManager/CreateStrategy/MainStrategy";
import { Roles } from "utils/creep/setups";
import { PositionHelper } from "utils/position/PositionHelper";

/**
 * 新增房间
 */
export class AddRoomDecision< T extends "AddRoomEvent"="AddRoomEvent"> extends ABaseDecision<T>
{
   
    public isExpired(event: AddRoomEvent, region: string): boolean 
    {
        return true;
    }
    public makeTask(event: AddRoomEvent, region: string): IDecisionCode<T>|false
    {
        global.Log.Info(`AddRoomDecision 更新房间 : ${JSON.stringify(event)}`);
        if(this.WorldGetter.EntityDB.getEntity("SpawnEntity",event.region))
        {
            global.Log.Warning(`添加新房间错误! 房间实体已经初始化完成${JSON.stringify(event)}`);
        }
        if(event.type == 1)
        {
            //初始化房间类型
            const room =Game.rooms[event.region];

            let center:[number,number]|undefined 
            // 新增主房间实体
            if(!Game.rooms[event.region].memory.center)
            {
                const plan = this.WorldGetter.Station.PollBlueplan(event.region);
                if(plan)
                {
                    const  plans = plan.getModulePlan("CoreEntity");
                    if(plans.length>0)
                    {
                        const link = plans[0].structures[STRUCTURE_LINK][0];
                        const storage = plans[0].structures[STRUCTURE_STORAGE][0];
                        const c = Math2D.multiply(Math2D.add(link,storage),0.5);
                        // center=[ plans[0].pos.x+1,plans[0].pos.y+1];
                        center=[c.x,c.y];
                    }
                    else
                    {
                        throw new Error(`${event.region}   :中心点未初始化`);
                    }
                }
                else
                {
                    throw new Error(`${event.region}   :布局未初始化`);

                }
            }
            

            // 确定前置都准备完成了。才能初始化内存
            const seqid = PositionHelper.seqId(room.name);
            if(!room.memory.seqId)
            {
                global.Log.Debug(`${room.name}`,`重新初始化房间`)
                room.memory ={
                    state:0,
                    config:{ type:1},Tasks:{} as any,Nodes:{},roleStatus:{} as any,idleCreeps:[],
                    lord:room.name,
                    history:{
                            time:Game.time,
                            level:room.controller?.level as number,
                            energyCapacity:room.energyCapacityAvailable,
                        },
                    seqId:seqid,
                    center:center,
                    stats:{
                         
                    } as any,
                    powerCreeps:[]
                    }
            }
 

            MainStrategy.initRoom(Game.rooms[event.region],this.WorldGetter.EntityDB);
            Game.rooms[event.region].controller!.initAttributes();
            if(!Memory.myrooms.includes(event.region))
            {
                Memory.myrooms.push(event.region)
            }
            
        }
        return false;
        // return code
    }
    
}